var is = 0;


layui.use(['table', 'admin', 'ax', 'func', 'element', 'form', 'layer'], function () {
        var $ = layui.$;
        var table = layui.table;
        var $ax = layui.ax;
        var admin = layui.admin;
        var func = layui.func;
        var element = layui.element;
        var form = layui.form;
        var layer = layui.layer;

        /**
         * 为数据库下拉框赋值
         */
        $.ajax(Feng.ctxPath + '/databaseListKh/listList', {
            success: function (data) {
                data = data.data;
                // console.log(data)
                $("#dbName").empty();
                $("#dbName").append("<option value=''>请选择数据库</option>");
                // console.log(data)
                for (let i = 0; i < data.length; i++) {
                    $("#dbName").append(new Option(data[i].dbName, data[i].id))
                }
                //重新渲染
                form.render("select");
            }
        })

        /**
         * 监听数据库下拉列表框,并为第二个下拉框赋值
         */
        form.on('select(dbName)', function (data) {
            ids = []
            // console.log(data)
            if (data.value == '') {
                $("#metadataName").empty();
                $("#metadataName").append("<option value=''>请选择元数据</option>");
                //重新渲染
                form.render("select");
            } else {
                $.ajax(Feng.ctxPath + '/dbServicedetails/listList', {
                    data: {
                        serviceId: data.value,
                    },
                    success: function (data) {
                        // console.log(data)
                        data = data.data
                        $("#metadataName").empty();
                        $("#metadataName").append("<option value=''>请选择元数据</option>");
                        // console.log(data)
                        for (let i = 0; i < data.length; i++) {
                            $("#metadataName").append(new Option(data[i].metadataName, data[i].metadataId))
                        }
                        //重新渲染
                        form.render("select");
                    }
                })
            }
        });

        form.on('select(metadataName)', function (data) {
            ids = []
            // console.log(data)
            if (data.value != "") {
                var queryData = {};
                queryData['metadataId'] = data.value;
                table.reload(MetadataCol.tableId, {
                    where: queryData, page: {curr: 1}
                });
            }
        });


        /**
         * 数据元表管理
         */
        var MetadataCol = {
            tableId: "metadataColTable"
        };

        /**
         * 初始化表格的列
         */
        MetadataCol.initColumn = function () {
            return [[
                {type: 'checkbox'},
                {field: 'metadataId', sort: false, title: '元数据编码', hide: true, align: "center"},
                {field: 'metadataColId', hide: true, title: '数据元编码', align: "center"},
                {field: 'columnName', sort: false, title: '字段中文名', align: "center"},
                {field: 'metadataColName', sort: false, title: '字段英文名', align: "center"},
                {field: 'type', sort: false, title: '字段类型', align: "center"},
                {field: 'metadataColNumber', sort: false, title: '数据长度', align: "center"},
                {
                    field: 'metadataDetailIsRequired',
                    sort: false,
                    title: '是否必填',
                    align: "center",
                    templet: function (d) {
                        if (d.isRequired == 1) {
                            return "是"
                        } else if (d.isRequired == 2) {
                            return "否"
                        }
                    }
                },
                {field: 'writeType', sort: false, title: '填写格式', align: "center"},
                {field: 'isDelete', sort: false, title: '是否删除；0代表删除；1代表可用', hide: true, align: "center"},
                {align: 'center', toolbar: '#tableBar', title: '操作', hide: true, align: "center"}
            ]];
        }
        ;

        // 渲染表格
        var tableResult = table.render({
            elem: '#' + MetadataCol.tableId,
            url: Feng.ctxPath + '/metadataCol/list',
            page: true,
            height: "full-158",
            cellMinWidth: 100,
            skin: "line",
            cols: MetadataCol.initColumn(),
            done: function (res, curr, count) {
                if (is == 0) {
                    var queryData = {};
                    queryData['metadataId'] = 0;
                    table.reload(MetadataCol.tableId, {
                        where: queryData, page: {curr: 1}
                    });
                    is = is + 1
                }
                $('tr').css('background-color', '#ffffff')
                //禁用全选
                // $('th[data-field=0] div').replaceWith('<div class="layui-table-cell laytable-cell-5-0-0"><span></span></div>');
                table_data = res.data;
            }
        });
        /**
         * 获取表格复选框内的值
         */
        var table_data = new Array();
        var ids = new Array();
        table.on('checkbox(' + MetadataCol.tableId + ')', function (obj) {
            // console.log(obj)
            if (obj.checked == true) {
                if (obj.type == 'one') {
                    ids.push(obj.data.metadataId);
                } else {
                    for (var i = 0; i < table_data.length; i++) {
                        ids.push(table_data[i].metadataId);
                    }
                }
            } else {
                if (obj.type == 'one') {
                    for (var i = 0; i < ids.length; i++) {
                        if (ids[i] == obj.data.metadataId) {
                            ids.splice(i, 1);
                        }
                    }
                } else {
                    for (var i = 0; i < ids.length; i++) {
                        for (var j = 0; j < table_data.length; j++) {
                            if (ids[i] == table_data[j].metadataId) {
                                ids.splice(i, 1);
                            }
                        }
                    }
                }
            }
        });

        $("#dataShift").click(function () {
            if (ids.length == 0) {
                Feng.error("请选择一条数据")
                return
            }
            if (ids.length != 1) {
                Feng.error("请选择一条数据")
                return
            }
            layer.open({
                title: "数据转换",
                type: 1,
                shade: 0.6,
                id: "",
                btn: ['转换', '取消'],
                btnAlign: 'c',
                moveType: 0,
                area: ["330px", "220px"],
                content: "<div class='layui-row' style='margin: 60px 0 0 0;padding: 0 0 0 0;text-align: center;font-size: 16px'><span>是否确定转换该条数据</span></div>",
                yes: function (index, layero) {
                    //发送请求
                    // console.log(ids[0])
                    $.ajax(Feng.ctxPath + '/dataShift/dataShift', {
                        data: {
                            metadataId: ids[0]
                        },
                        success: function (data) {
                            if (data == 200) {
                                Feng.success("发送转换请求成功,耐心等待转换过程")
                                layer.closeAll()
                                table.reload(MetadataCol.tableId)
                                ids = []
                            }
                            if (data == 500) {
                                Feng.error("发送转换请求失败,请稍候再试")
                                layer.closeAll()
                                table.reload(MetadataCol.tableId)
                                ids = []
                            }
                        }
                    })
                },
                end: function () {
                    table.reload(MetadataCol.tableId)
                    ids = []
                }
            })


            // layer.open({
            //     title: "数据转换",
            //     type: 1,
            //     shade: 0.6,
            //     id: "",
            //     btn: ['转换', '取消'],
            //     btnAlign: 'c',
            //     moveType: 0,
            //     area: ["400px", "300px"],
            //     content: "<div class='layui-row layui-form' style='margin:80px 0 0 0;text-align: center'>" +
            //         "<div class='layui-form-item'>" +
            //         "<div class='layui-inline'>" +
            //         "<select name='dataShift1' id='dataShift1' lay-filter='dataShift1'>" +
            //         "<option value=''>请选择转换规则</option>" +
            //         "</select>" +
            //         "</div>" +
            //         "</div>" +
            //         "</div>",
            //     success: function (layero, index) {
            //         form.render();
            //         $.ajax(Feng.ctxPath + '/standardManagement/list', {
            //             success: function (data) {
            //                 data = data.data
            //                 // console.log(data)
            //                 $("#dataShift1").empty();
            //                 $("#dataShift1").append("<option value=''>请选择转换规则</option>");
            //                 // console.log(data)
            //                 for (let i = 0; i < data.length; i++) {
            //                     $("#dataShift1").append(new Option(data[i].dataStandardName, data[i].funId))
            //                 }
            //                 //重新渲染
            //                 form.render("select");
            //             }
            //         })
            //         var btn = layero.find('.layui-layer-btn');
            //         btn.find('.layui-layer-btn0').click(function () {
            //             //获取选中数据
            //             var data = $("#dataShift1").val()
            //             alert(data)
            //         })
            //     },
            //     end:function () {
            //         table.reload(MetadataCol.tableId)
            //         ids = []
            //     }
            // })
        })


    }
);
